# -*- coding: utf-8 -*-

"""
* Copyright (C) 2016 GridSafe, Inc.
"""
from apps.wallet.models.profile import Profile
from rest_framework.exceptions import NotFound



class ProfileService(object):
    def __init__(self, user_id):
        try:
            self._profile = Profile.objects.get(user__id=user_id)
        except Profile.DoesNotExist:
            raise NotFound('用户信息不存在')

    @staticmethod
    def list():
        return Profile.objects.all()

    def update(self, **kwargs):
        fields = [
            'user', 'name', 'sex', 'career', 'balance',
            'statistic', 'app_id', 'app_key', 'app_secret',
            'description'
        ]
        result = set(kwargs.keys()).intersection(set(fields))
        if result:
            raise TypeError('{} got unexpected key arguments [{}] '.format(
                self.update.__name__, result))
        else:
            if 'statistic' in kwargs.keys():
                balance = kwargs.pop('balance')
                delta = balance - self._profile.balance_display

            Profile.objects.filter(id=self._profile.id).update(**kwargs)



